package com.zjweu.hkg.DAO;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;

public class ArticleSqlProvider {

    public String getArticlesByTagsSql(List<String> tags) {
        SQL sql = new SQL();
        sql.SELECT("a.article_id, a.article_title, GROUP_CONCAT(t.tag_content) as tags");
        sql.FROM("article a");
        sql.JOIN("article_tags at ON a.article_id = at.article_id");
        sql.JOIN("tag t ON at.tag_id = t.tag_id");

        StringBuilder conditions = new StringBuilder();
        for (int i = 0; i < tags.size(); i++) {
            if (i > 0) {
                conditions.append(" OR ");
            }
            conditions.append("t.tag_content LIKE #{tags[").append(i).append("]}");
        }
        sql.WHERE(conditions.toString());
        sql.GROUP_BY("a.article_id");

        return sql.toString();
    }
}